<template>
	<section>
		<!--模糊查询工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.username" clearable placeholder="登录帐号"></el-input>
				</el-form-item>
				<!---------按钮 ↓----------->
				<el-form-item>
					<el-button type="primary" v-on:click="getEmployee">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<!--		:data :绑定的数据-->
		<!--		v-loading: 页面css样式等东西加载之后加载加载数据-->
		<!--		prop:对应的是:data绑定的对象的属性-->
		<!--		label :描述-->
		<el-table :data="dept" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="id" label="员工ID" width="120" sortable>
			</el-table-column>
			<el-table-column prop="username" label="登录帐号" width="120" sortable>
			</el-table-column>
			<el-table-column prop="password" label="密码" width="120" sortable>
			</el-table-column>
			<el-table-column prop="username" label="邮箱" width="120" sortable>
			</el-table-column>
			<el-table-column prop="email" label="电话" width="120" sortable>
			</el-table-column>
			<el-table-column prop="age" label="年龄" width="120" sortable>
			</el-table-column>
			<el-table-column prop="state" label="状态" width="100" :formatter="formatSex" sortable>
				<template slot-scope="scope">
					<el-button type="text" size="small"
							   :type="scope.row.state === 1?'primary':'danger'"
					>
						{{scope.row.state === 1 ?"启用":"禁用"}}
					</el-button>
				</template>
			</el-table-column>

			<el-table-column prop="managerName" label="部门经理" width="100" sortable>
			</el-table-column>
			<el-table-column prop="parentName" label="上级部门" width="100" sortable>
			</el-table-column>

			<el-table-column label="操作" width="150">
				<template slot-scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--分页条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<!--			current-change:自带的事件,我当前页发生改变时,就会触发此事件-->
			<el-pagination layout="sizes,prev, pager, next" @current-change="handleCurrentChange" :page-size="5" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog title="操作" :visible.sync="editFormVisible"  :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="帐号" prop="username">
					<el-input v-model="editForm.username" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="密码" prop="password">
					<el-input v-model="editForm.password" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="邮箱" prop="email">
					<el-input v-model="editForm.email" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="电话" prop="phone">
					<el-input v-model="editForm.phone" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="年龄" prop="age">
					<el-input v-model="editForm.age" auto-complete="off"></el-input>
				</el-form-item>
				<!-- 部门状态  -->
				<el-form-item label="" prop="state">
					<el-switch
							style="display: block"
							v-model="state"
							active-color="#13ce66"
							inactive-color="#ff4949"
							active-text="状态:启用"
							inactive-text="状态:禁用">
					</el-switch>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>

	//import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	//import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

	import {batchRemoveUser} from "../../api/api";

	export default {
		data() {
			return {
				employeeAll: [],      //所有员工的数据源
				filters: {
					age: '',
					username: ''
				},
				dept: [],
				total: 0,
				page: 1,
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入', trigger: 'blur' }
					]
				},
				//编辑界面数据
				editForm: {
					id:'',
					username:'',
					email:'',
					phone:'',
					password:'',
					age:''
				},
				state: true,          // 部门状态
				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
					name: [
						{ required: true, message: '请输入', trigger: 'blur' }
					]
				},
			}
		},
		methods: {
			//性别显示转换
			//row 当前行row = user  column当前列
			formatSex: function (row, column) {
				return row.state == 1 ? '正常' : row.state == 0 ? '禁用' : '未知';
			},
			handleCurrentChange(val) {
				//var 就是当前页的数据
				this.page = val;
				this.getEmployee();
			},
			//获取所有员工数据列表
			getEmployee() {
				//封装的数据
				let para = {
					//page 就是当前页undefine
					"page": this.page,
					//这个是高级查询的条件
					"keyword": this.filters
				};
				console.log("------我是模糊查询的值----------")
				console.log(para)
				 para.employee.age=this.filters.age;
				//para.employee.age=Number(this.filters.age);
				this.listLoading = true;
				//NProgress.start();
				this.$http.post("/mytest/pagelist",para).then((res) => {
					// console.log(typeof res.data.list[0].age)
					//console.log(typeof JSON.stringify(res.data.list[0].age));
					//console.log("后端发送过来的分页数据:"+JSON.stringify(res));

					this.total = res.data.total;
					//
					this.dept = res.data.list;
					//关闭进度条
					this.listLoading = false;
					//NProgress.done();
				});
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					//let para = { id: row.id };
					this.$http.delete("/user/"+row.id).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getEmployee();
					});
				}).catch(() => {

				});
			},
			/* 回显 -----------------------------------   */
			//显示编辑界面
			handleEdit: function (index, row) {
				//显示编辑页面的模态框
				this.editFormVisible = true;
				//Object.assign 是拷贝对象 BeanUtil.copyof()
				this.editForm = row;
				// 回显时将Integer的值装换为Boolean 进行动态回显
				//  方式一:  this.state = row.state? true:false
				this.state = !!row.state;
				console.log("当前修改的数据是:"+JSON.stringify(this.editForm))
			},
			//显示新增界面
			handleAdd: function () {
				this.editFormVisible = true;
				/*  清空新增界面的回显数据  */
				this.editForm = {
					id:'',
					username:'',
					email:'',
					phone:'',
					password:'',
					age:''
				};
				// 将状态码的默认值设置为true
				this.state = true
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							//这里面是获取数据的
							this.editLoading = true;
							//拷贝对象
							let para = Object.assign({}, this.editForm);
							// 传值的时候将前端的Boolean 通过三目转换为 Integer 来进行回传  为了统一回显的数据类型
							para.state = this.state? 1:0
							this.$http.post("/user/save",para).then((res) => {
								this.editLoading = false;
								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['editForm'].resetFields();
								this.editFormVisible = false;
								this.getEmployee();
							});
						});
					}
				});
			},
			//复选勾选框的方法
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				let ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { ids: ids };
              	    console.log("-------批量删除的字段-----------")
					console.log(para)
					this.$http.post("/user/bathDelete",para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getEmployee();
					});
				}).catch(() => {

				});
			}
		},
		//就是页面已加载完成就会去触发这个mounted
		mounted() {
			//获取所有员工的列表
			this.getEmployee();
		}
	}

</script>

<style scoped>

</style>
